import axios from "axios";
import qs from "qs";
import axiosInstance from "../utils/axiosInstances";
import { PaginationParams, PaginationResult } from './types'
/**
 * 获取设备类型列表
 */
export function listGroupAll(params: any) {
    return axiosInstance.get("/system/group/listAll", {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        }
    });
}
export function listDevice(){
    return axiosInstance.get("/system/device/getPVTab");
}

export function listFusionGroup(params:any){
    return axiosInstance.get("/system/device/listFusionGroup",{
        params,
        paramsSerializer:(obj)=>{
            return qs.stringify(obj);
        }
    });
}

/**
 * 变量类型枚举
 */
export enum VariableType {
    ANALOG = 1, // 模拟量
    DIGITAL = 2, // 状态量
}

/**
 * 变量状态枚举
 */
export enum VariableStatus {
    ENABLED = 0, // 启用
    DISABLED = 1, // 停用
}

/**
 * 数据来源类型枚举
 */
export enum DataSourceType {
    IO_TYPE = 1, // IO型
    OTHER = 2,    // 其他
}

/**
 * 数据类型枚举
 */
export enum DataDataType {
    FLOAT_4BYTE = 19, // 4字节浮点4321
}

/**
 * 设备变量基础信息
 */
export interface DeviceVariable {
    id: string | number;
    deviceId: string | number;
    deviceName: string;
    channelId: string | number;
    channelName: string;
    varName: string;
    varSn: string;
    varType: VariableType;
    stopFlag: VariableStatus;
    origin: DataSourceType;
    messageAddress: string;
    dataType: DataDataType;
    initValue: string;
    baseValue: number;
    coefficient: number;
    saveCycle: number;
    isAccumulation: number;
    dataFix: number;
    createBy: string;
    createTime: string;
    updateBy: string;
    updateTime: string;
}

/**
 * 设备树节点
 */
export interface DeviceTreeNode {
    id: string | number;
    parentId: string | number;
    name: string;
    title: string;
    key: string;
    children?: DeviceTreeNode[];
    selectable?: boolean;
    isGroup?: boolean;
    deviceSn?: string;
}

/**
 * 设备变量查询参数
 */
export interface DeviceVariableQueryParams extends PaginationParams {
    deviceId?: string | number;
    varName?: string;
    varType?: VariableType;
    stationType?: number;
}

/**
 * 变量列表
 * @param params 查询参数
 * @returns 变量分页列表
 */
export function listDeviceVar(params: DeviceVariableQueryParams) {
    return axiosInstance.get<PaginationResult<DeviceVariable>>("/system/device-var/list", {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 获取设备树数据
 * @returns 设备树节点列表
 */
export function fetchDeviceData(stationType = 2) {
    return axiosInstance.get<DeviceTreeNode[]>("/system/device/tree", {
        params: { stationType }
    });
}

/**
 * 新增设备变量
 * @param data 变量数据
 * @returns 操作结果
 */
export function addDeviceVar(data: DeviceVariable) {
    return axiosInstance.post("/system/device-var/add", data);
}

/**
 * 更新设备变量
 * @param data 变量数据
 * @returns 操作结果
 */
export function updateDeviceVar(data: DeviceVariable) {
    return axiosInstance.post("/system/device-var/update", data);
}

/**
 * 删除设备变量
 * @param id 变量ID
 * @returns 操作结果
 */
export function deleteDeviceVar(id: string | number) {
    return axiosInstance.post("/system/device-var/delete", { id });
}

/**
 * 批量删除设备变量
 * @param ids 变量ID列表
 * @returns 操作结果
 */
export function batchDeleteDeviceVars(ids: (string | number)[]) {
    return axiosInstance.post("/system/device-var/batch-delete", { ids });
}